#pragma once
#include "stdafx.h"

namespace namespace_Main_Window
{
	#define MAX_LOADSTRING 100

	LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

	class Main_Window
	{
	public:
		HINSTANCE	hInst;										// current instance
		TCHAR		szTitle[MAX_LOADSTRING];					// The title bar text
		TCHAR		szWindowClass[MAX_LOADSTRING];				// the main window class name
		HWND		hWnd;
		MSG			msg;

		bool		Exit;
		int			x_Coord, 
					y_Coord, 
					Width, 
					Height;

	
		BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
		{
		   hInst = hInstance;

		   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPED | WS_MINIMIZE | WS_SYSMENU,
			  x_Coord, y_Coord, Width, Height, NULL, NULL, hInstance, NULL);

		   if (!hWnd)	return FALSE;
   
		   ShowWindow	(hWnd, SW_SHOWDEFAULT);
		   UpdateWindow	(hWnd);

		   return TRUE;
		}
		ATOM MyRegisterClass(HINSTANCE hInstance)
		{
			WNDCLASSEX wcex;

			wcex.cbSize = sizeof(WNDCLASSEX);

			wcex.style			= WS_OVERLAPPED;
			wcex.lpfnWndProc	= WndProc;
			wcex.cbClsExtra		= 0;
			wcex.cbWndExtra		= 0;
			wcex.hInstance		= hInstance;
			wcex.hIcon			= NULL;
			wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
			wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
			wcex.lpszMenuName	= NULL;
			wcex.lpszClassName	= szWindowClass;
			wcex.hIconSm		= NULL;

			return RegisterClassEx(&wcex);
		}

		Main_Window()
		{
			Exit	=	false;
			x_Coord	=	50;
			y_Coord	=	50;
			Height	=	500;
			Width	=	500;
		}
		Main_Window(LPWSTR WinName,int x, int y, int height, int width)
		{
			wcscpy(szWindowClass,WinName);
			wcscpy(szTitle,WinName);
			Exit	=	false;
			x_Coord	=	x;
			y_Coord	=	y;
			Height	=	height;
			Width	=	width;
		}


		void init(HINSTANCE hInstance,
						 HINSTANCE hPrevInstance,
						 LPTSTR    lpCmdLine,
						 int       nCmdShow)
		{
			UNREFERENCED_PARAMETER(hPrevInstance);
			UNREFERENCED_PARAMETER(lpCmdLine);

			MyRegisterClass(hInstance);

			if (!InitInstance (hInstance, nCmdShow))	return;
		}

		int Run()
		{
			while(!Exit)
			{
				MyGame.KeyboardScanner();

				if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
				{
					TranslateMessage(&msg);
					DispatchMessage(&msg);
				}
		
				MyGame.Render();
			}
			DestroyWindow(hWnd);
			return (int) msg.wParam;
		}

		void FinishProgramm()
		{
			Exit	=	true;
		}
		bool Render()
		{
			return Game.Render();
		}
		void MessageFilter(UINT message, WPARAM wParam, LPARAM lParam)
		{
			switch (message)
			{
			case WM_MOUSEWHEEL:		
				MyGame.MouseWheel((short)HIWORD(wParam)>0);
				break;
			case WM_MOUSEMOVE:
				MyGame.MouseMove();
				break;
			case WM_LBUTTONDOWN:
				MyGame.MouseDown();
				break;
			case WM_LBUTTONUP:
				MyGame.MouseUp();
				break;
			}
		}
	};

	Main_Window		Window;

	LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
	{
		int wmId, wmEvent;
		Window.MessageFilter(message, wParam, lParam);

		switch (message)
		{
		case WM_COMMAND:
			wmId    = LOWORD(wParam);
			wmEvent = HIWORD(wParam);
			switch (wmId)
			{
			default:
				return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_PAINT:
			Window.Render();
			ValidateRect(hWnd,NULL);
			break;
		case WM_DESTROY:
			Window.FinishProgramm();
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		return 0;
	}
}